2018.12.3

plotly

plotly 簡介

  • Plotly是一個資料視覺化的R套件,以簡單的方式,讓資料能夠產生互動的效果。
  • 提供一個合作平台,使用者能夠將自己在R中繪製的圖存上屬於自己的Plotly平台上。
  • 結合了各式各樣的API,包裝PythonRMatlab、…等等
  • 當然~ggplot2也能夠輕易地使用plotly轉換成互動式的圖表!!

想知道更多~

畫圖前的準備

  • 安裝套件
install.packages(c("plotly"))


  • 載入套件
library(plotly)


基本架構

  • plot_ly()

  • 注意:

    • %>%連接

    • 指定變數前須加~

ggplotly()

  • ggplot可以使用plotly換成互動式的圖表
temp ##ggplot的圖 
ggplotly(temp)

histogram - ggplotly

  • ex : 交易房屋的屋齡狀況
temp<-transaction %>% ggplot(aes(x=age, y =..count..)) + 
  geom_histogram(colour="grey",fill = "steelblue",binwidth = 5)+
  labs(title="交易房屋的屋齡狀況",x="屋齡",y="數量")
ggplotly(temp)

histogram - plot_ly

  • ex : 交易房屋的屋齡狀況
transaction %>% plot_ly(x = ~age, type = "histogram")

boxplot - ggplotly

  • ex:呈現各縣市每平方公尺價格
temp<-transaction %>% ggplot(aes(x=city,y=price_unit)) + 
  geom_boxplot() + 
  labs(title="各縣市每平方公尺價格",x = "縣市" , y = "每平方公尺價格(元)")
ggplotly(temp)

boxplot - plot_ly

  • ex:呈現各縣市每平方公尺價格
transaction %>% plot_ly(x = ~price_unit, y = ~use_type) %>% 
  add_boxplot(color = ~use_type) %>%
  layout(yaxis = list(title = ""), margin = list(l = 100))

boxplot - plot_ly

  • ex:呈現各縣市各使用分區每平方公尺價格

  • y = ~interaction(use_type,city)

transaction %>% plot_ly(x = ~price_unit, y = ~interaction(use_type,city)) %>% 
  add_boxplot(color = ~use_type) %>%
  layout(yaxis = list(title = ""), margin = list(l = 100))

scatter plot - ggplotly

  • ex : iris花瓣長度(Petal.Length)和花萼長度(Sepal.Length)關係
temp<-ggplot(iris , aes(x=Sepal.Length, y=Petal.Length, color=Species)) + 
  geom_point(shape=1, size=2)+# shape控制圖示;size控制點的大小
  labs(title="花萼和花瓣長度",x="花萼長度",y="花瓣長度")
ggplotly(temp)

scatter plot - plot_ly

  • ex : iris花瓣長度(Petal.Length)和花萼長度(Sepal.Length)關係
iris %>% 
  plot_ly (
  x = ~Sepal.Length ,
  y = ~Petal.Length ,
  color = ~Species,
  type = 'scatter',
  mode = 'markers')

highcharter

highcharter 簡介

畫圖前的準備

  • 安裝套件
install.packages(c("highcharter"))


  • 載入套件
library(highcharter)


基本語法

highchart() %>% 
  hc_title(text = "...") %>% 
  hc_xAxis(categories = ...$...) %>% 
  hc_add_series(name = "...", data =...$...,color="...") %>% 
  hc_add_series(name = "...", data = ...$...,color="...")

注意 :

  • 使用寬資料

  • 指定變數需放置data$變數

  • %>%連接

折線圖

ex : 各縣市的各月份的交易量

檢視資料:

count_month_wide<-spread(count_month, key = city, value = n)
     city trac_month    n
1  高雄市          1 2511
2  高雄市          2 1897
3  高雄市          3 3355
4  高雄市          4 3524
5  高雄市          5 3378
6  高雄市          6 3097
7  高雄市          7 3063
8  高雄市          8 2573
9  高雄市          9 2354
10 高雄市         10 2590
11 高雄市         11 2454
12 高雄市         12 3664
13 新北市          1 3664
14 新北市          2 2714
15 新北市          3 5328
16 新北市          4 5350
17 新北市          5 5656
18 新北市          6 4999
19 新北市          7 5636
20 新北市          8 4149
21 新北市          9 3951
22 新北市         10 5346
23 新北市         11 5401
24 新北市         12 5224
25 臺中市          1 2734
26 臺中市          2 2062
27 臺中市          3 3664
28 臺中市          4 3448
29 臺中市          5 3545
30 臺中市          6 3137
31 臺中市          7 3142
32 臺中市          8 3176
33 臺中市          9 2571
34 臺中市         10 3226
35 臺中市         11 3112
36 臺中市         12 3665
37 臺北市          1 1458
38 臺北市          2 1198
39 臺北市          3 2176
40 臺北市          4 2360
41 臺北市          5 2500
42 臺北市          6 2159
43 臺北市          7 1964
44 臺北市          8 1816
45 臺北市          9 1938
46 臺北市         10 2004
47 臺北市         11 2012
48 臺北市         12 2653
   trac_month 高雄市 新北市 臺中市 臺北市
1           1   2511   3664   2734   1458
2           2   1897   2714   2062   1198
3           3   3355   5328   3664   2176
4           4   3524   5350   3448   2360
5           5   3378   5656   3545   2500
6           6   3097   4999   3137   2159
7           7   3063   5636   3142   1964
8           8   2573   4149   3176   1816
9           9   2354   3951   2571   1938
10         10   2590   5346   3226   2004
11         11   2454   5401   3112   2012
12         12   3664   5224   3665   2653

  • ex : 呈現各縣市各月份的交易量
highchart() %>% 
  hc_title(text = "各縣市的各月份的交易量") %>% 
  hc_xAxis(categories = count_month$trac_month) %>% #以month當x軸
  hc_add_series(name = "臺北市", data =count_month_wide$`臺北市`,color="#006BA6") %>% #第一條線
  hc_add_series(name = "新北市", data =count_month_wide$`新北市`,color="#FFBC42") %>% #第二條線
  hc_add_series(name = "高雄市", data =count_month_wide$`高雄市`,color="#D81159") %>% #第三條線
  hc_add_series(name = "臺中市", data =count_month_wide$`臺中市`,color="#8F2D56") #第四條線

  • ex : 呈現各縣市各月份的交易量

長條圖

  • ,type = "column" : 圖形由折線圖改為長條圖

ex : 各縣市的各使用分區的交易量

count_city_use_wide<-spread(data = count_city_use[,c(1:3)], key = use_type, value = n_use)
     city use_type n_use     n  rate
1  高雄市       住 26027 34460 0.755
2  高雄市       商  7582 34460 0.220
3  高雄市     其他   532 34460 0.015
4  高雄市       工   196 34460 0.006
5  高雄市       農   123 34460 0.004
6  新北市       住 44129 57418 0.769
7  新北市       商  6533 57418 0.114
8  新北市     其他  4326 57418 0.075
9  新北市       工  2361 57418 0.041
10 新北市       農    69 57418 0.001
11 臺中市       住 30460 37482 0.813
12 臺中市       商  4767 37482 0.127
13 臺中市     其他  1639 37482 0.044
14 臺中市       農   382 37482 0.010
15 臺中市       工   234 37482 0.006
16 臺北市       住 14761 24238 0.609
17 臺北市       商  7323 24238 0.302
18 臺北市     其他  1709 24238 0.071
19 臺北市       工   442 24238 0.018
20 臺北市       農     3 24238 0.000
    city   工    住 其他   商  農
1 高雄市  196 26027  532 7582 123
2 新北市 2361 44129 4326 6533  69
3 臺中市  234 30460 1639 4767 382
4 臺北市  442 14761 1709 7323   3

ex : 各縣市的各使用分區的交易量

highchart() %>% 
  hc_title(text = "各縣市的各使用分區的交易量") %>% 
  hc_xAxis(categories = factor(count_city_use_wide$city)) %>% #以city當x軸
  hc_add_series(name = "工", data =count_city_use_wide$`工`,color="#006BA6",type = "column") %>% #第一條線
  hc_add_series(name = "住", data =count_city_use_wide$`住`,color="#FFBC42",type = "column") %>% #第二條線
  hc_add_series(name = "其他", data =count_city_use_wide$`農`,color="#D81159",type = "column") %>% #第三條線
  hc_add_series(name = "商", data =count_city_use_wide$`商`,color="#8F2D56",type = "column") #第四條線

ex : 各縣市的各使用分區的交易量

顯示文字

  • ,dataLabels = list(enabled = T) : 顯示數字
highchart() %>% 
  hc_title(text = "各縣市的各使用分區的交易量") %>% 
  hc_xAxis(categories = count_city_use_wide$city) %>% #以month當x軸
  hc_add_series(name = "工", data =count_city_use_wide$`工`,color="#006BA6",type = "column", dataLabels = list(enabled = T)) %>% #第一條線
  hc_add_series(name = "住", data =count_city_use_wide$`住`,color="#FFBC42",type = "column", dataLabels = list(enabled = T)) %>% #第二條線
  hc_add_series(name = "其他", data =count_city_use_wide$`農`,color="#D81159",type = "column", dataLabels = list(enabled = T)) %>% #第三條線
  hc_add_series(name = "商", data =count_city_use_wide$`商`,color="#8F2D56",type = "column", dataLabels = list(enabled = T)) #第四條線